package com.ambow.shop.controller;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.shop.action.Action;
import com.ambow.shop.action.impl.ShowHomePageAction;

/**
 * 首页控制器。
 * 
 * @author chunw
 *
 */
@WebServlet("/home")
public class HomeController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public HomeController() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}

	private String getActionName(String uri) {
		return uri.substring(uri.lastIndexOf("/") + 1);
	}

	private void process(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Action action = null;
		String actionName = getActionName(request.getRequestURI());
		if (actionName.equals("home")) {
			action = new ShowHomePageAction();
		}
		String result = action.execute(request, response);
		request.getRequestDispatcher(result).forward(request, response);
	}

}
